import tkinter as tk
from tkinter import *
from tkinter import messagebox


class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.master = master
        self.pack()

        self.createWidget()

    def createWidget(self):
        '''创建登录界面的组件'''
        # 创建登录框
        self.label01 = Label(self, text='用户名')
        self.label01.pack()
        # StringVar变量绑定到指定的组件
        # StringVar变量的值发生改变，组件内容也变化
        # 组件内容发生变化，StringVar变量的值也发生变化
        v1 = StringVar()
        self.entry01 = Entry(self, textvariable=v1)
        self.entry01.pack()
        print(v1.get())  # 获取输入的用户名并打印

        # 创建密码框
        self.label02 = Label(self, text='密码')
        self.label02.pack()
        v2 = StringVar()
        self.entry02 = Entry(self, textvariable=v2, show='*')
        self.entry02.pack()

        Button(self, text='登录', command=self.login).pack()

    def login(self):
        username = self.entry01.get()
        pwd = self.entry02.get()

        if username == 'QinAfeng' and pwd == '123456':
            messagebox.showinfo('TK世界', '恭喜你登录成功')
        else:
            messagebox.showinfo('TK世界', '登录失败！用户名或密码错误！')


if __name__ == '__main__':
    root = Tk()
    # root.geometry('200x200+200+300')
    # 设置窗口大小
    width = 500
    height = 300
    # 获取屏幕尺寸以计算布局参数，使窗口居屏幕中央
    screenwidth = root.winfo_screenwidth()
    screenheight = root.winfo_screenheight()
    alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 3)
    root.geometry(alignstr)
    root.title('Entry实现登录')
    app = Application(root)
    root.mainloop()